package cringer::translate_tts;

# translate_tts.pm
# Uses Google's Translation TTS to make cringer say out loud what is being chatted about in the channel.
# This module requires Audio::Play::MPG123 as well as having mpg123 installed.
# Your bot's main script must initialize the audio player as well:

# eg: $self->{player} = Audio::Play::MPG123->new();

our $VERSION = 2.0;

sub said{
 my $self = $_[0];
 my $args = $_[1];
 my $me = $self->{nick};

 my $url = URI->new( "http://209.85.129.101/translate_tts" );
 if (length($args->{body}) < 100 ){
  $url->query_form( 'tl' => 'en', 'q' => "$args->{who} . $args->{body}" );
  $self->{player}->poll(0) until $self->{player}->state == 0;
  $self->{player}->load($url);
  $self->{player}->poll(1) until $self->{player}->state == 0;
 }
 else {
  my @bits = split(/,/,$args->{body});
  my $counter = 1;
  my $first = shift @bits;
  $url->query_form( 'tl' => 'en', 'q' => "$args->{who}. $first" );
  $self->{player}->poll(0) until $self->{player}->state == 0;
  $self->{player}->load($url);
  $self->{player}->poll(1) until $self->{player}->state == 0;
  foreach my $phrase (@bits){
   $counter++;
   $url->query_form( 'tl' => 'en', 'q' => "$phrase" );
   $self->{player}->poll(0) until $self->{player}->state == 0;
   $self->{player}->load($url);
   $self->{player}->poll(1) until $self->{player}->state == 0;
  }
 }
return 0; # don't say stupid stuff, like "256", or "1"
}
